<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>group.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_graphics_ext_group.js.html">group.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>
<a name="line16"></a>/**
<a name="line17"></a> * @fileoverview A thicker wrapper around graphics groups.
<a name="line18"></a> * @author robbyw@google.com (Robby Walker)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.graphics.ext.Group&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.graphics.ext.Element&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>/**
<a name="line29"></a> * Wrapper for a graphics group.
<a name="line30"></a> * @param {goog.graphics.ext.Group} group Parent for this element. Can
<a name="line31"></a> *     be null if this is a Graphics instance.
<a name="line32"></a> * @param {goog.graphics.GroupElement=} opt_wrapper The thin wrapper
<a name="line33"></a> *     to wrap. If omitted, a new group will be created. Must be included
<a name="line34"></a> *     when group is null.
<a name="line35"></a> * @constructor
<a name="line36"></a> * @extends {goog.graphics.ext.Element}
<a name="line37"></a> */
<a name="line38"></a>goog.graphics.ext.Group = function(group, opt_wrapper) {
<a name="line39"></a>  opt_wrapper = opt_wrapper || group.getGraphicsImplementation().createGroup(
<a name="line40"></a>      group.getWrapper());
<a name="line41"></a>  goog.graphics.ext.Element.call(this, group, opt_wrapper);
<a name="line42"></a>
<a name="line43"></a>  /**
<a name="line44"></a>   * Array of child elements this group contains.
<a name="line45"></a>   * @type {Array.&lt;goog.graphics.ext.Element&gt;}
<a name="line46"></a>   * @private
<a name="line47"></a>   */
<a name="line48"></a>  this.children_ = [];
<a name="line49"></a>};
<a name="line50"></a>goog.inherits(goog.graphics.ext.Group, goog.graphics.ext.Element);
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * Add an element to the group.  This should be treated as package local, as
<a name="line55"></a> * it is called by the draw* methods.
<a name="line56"></a> * @param {!goog.graphics.ext.Element} element The element to add.
<a name="line57"></a> * @param {boolean=} opt_chain Whether this addition is part of a longer set
<a name="line58"></a> *     of element additions.
<a name="line59"></a> */
<a name="line60"></a>goog.graphics.ext.Group.prototype.addChild = function(element, opt_chain) {
<a name="line61"></a>  if (!goog.array.contains(this.children_, element)) {
<a name="line62"></a>    this.children_.push(element);
<a name="line63"></a>  }
<a name="line64"></a>
<a name="line65"></a>  var transformed = this.growToFit_(element);
<a name="line66"></a>
<a name="line67"></a>  if (element.isParentDependent()) {
<a name="line68"></a>    element.parentTransform();
<a name="line69"></a>  }
<a name="line70"></a>
<a name="line71"></a>  if (!opt_chain &amp;&amp; element.isPendingTransform()) {
<a name="line72"></a>    element.reset();
<a name="line73"></a>  }
<a name="line74"></a>
<a name="line75"></a>  if (transformed) {
<a name="line76"></a>    this.reset();
<a name="line77"></a>  }
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Remove an element from the group.
<a name="line83"></a> * @param {goog.graphics.ext.Element} element The element to remove.
<a name="line84"></a> */
<a name="line85"></a>goog.graphics.ext.Group.prototype.removeChild = function(element) {
<a name="line86"></a>  goog.array.remove(this.children_, element);
<a name="line87"></a>
<a name="line88"></a>  // TODO(robbyw): shape.fireEvent(&#39;delete&#39;)
<a name="line89"></a>
<a name="line90"></a>  this.getGraphicsImplementation().removeElement(element.getWrapper());
<a name="line91"></a>};
<a name="line92"></a>
<a name="line93"></a>
<a name="line94"></a>/**
<a name="line95"></a> * Calls the given function on each of this component&#39;s children in order.  If
<a name="line96"></a> * {@code opt_obj} is provided, it will be used as the &#39;this&#39; object in the
<a name="line97"></a> * function when called.  The function should take two arguments:  the child
<a name="line98"></a> * component and its 0-based index.  The return value is ignored.
<a name="line99"></a> * @param {Function} f The function to call for every child component; should
<a name="line100"></a> *    take 2 arguments (the child and its index).
<a name="line101"></a> * @param {Object=} opt_obj Used as the &#39;this&#39; object in f when called.
<a name="line102"></a> */
<a name="line103"></a>goog.graphics.ext.Group.prototype.forEachChild = function(f, opt_obj) {
<a name="line104"></a>  if (this.children_) {
<a name="line105"></a>    goog.array.forEach(this.children_, f, opt_obj);
<a name="line106"></a>  }
<a name="line107"></a>};
<a name="line108"></a>
<a name="line109"></a>
<a name="line110"></a>/**
<a name="line111"></a> * @return {goog.graphics.GroupElement} The underlying thin wrapper.
<a name="line112"></a> * @protected
<a name="line113"></a> */
<a name="line114"></a>goog.graphics.ext.Group.prototype.getWrapper;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Reset the element.
<a name="line119"></a> * @override
<a name="line120"></a> */
<a name="line121"></a>goog.graphics.ext.Group.prototype.reset = function() {
<a name="line122"></a>  goog.graphics.ext.Group.superClass_.reset.call(this);
<a name="line123"></a>
<a name="line124"></a>  this.updateChildren();
<a name="line125"></a>};
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Called from the parent class, this method resets any pre-computed positions
<a name="line130"></a> * and sizes.
<a name="line131"></a> * @protected
<a name="line132"></a> * @override
<a name="line133"></a> */
<a name="line134"></a>goog.graphics.ext.Group.prototype.redraw = function() {
<a name="line135"></a>  this.getWrapper().setSize(this.getWidth(), this.getHeight());
<a name="line136"></a>  this.transformChildren();
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * Transform the children that need to be transformed.
<a name="line142"></a> * @protected
<a name="line143"></a> */
<a name="line144"></a>goog.graphics.ext.Group.prototype.transformChildren = function() {
<a name="line145"></a>  this.forEachChild(function(child) {
<a name="line146"></a>    if (child.isParentDependent()) {
<a name="line147"></a>      child.parentTransform();
<a name="line148"></a>    }
<a name="line149"></a>  });
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * As part of the reset process, update child elements.
<a name="line155"></a> */
<a name="line156"></a>goog.graphics.ext.Group.prototype.updateChildren = function() {
<a name="line157"></a>  this.forEachChild(function(child) {
<a name="line158"></a>    if (child.isParentDependent() || child.isPendingTransform()) {
<a name="line159"></a>      child.reset();
<a name="line160"></a>    } else if (child.updateChildren) {
<a name="line161"></a>      child.updateChildren();
<a name="line162"></a>    }
<a name="line163"></a>  });
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * When adding an element, grow this group&#39;s bounds to fit it.
<a name="line169"></a> * @param {!goog.graphics.ext.Element} element The added element.
<a name="line170"></a> * @return {boolean} Whether the size of this group changed.
<a name="line171"></a> * @private
<a name="line172"></a> */
<a name="line173"></a>goog.graphics.ext.Group.prototype.growToFit_ = function(element) {
<a name="line174"></a>  var transformed = false;
<a name="line175"></a>
<a name="line176"></a>  var x = element.getMaxX();
<a name="line177"></a>  if (x &gt; this.getWidth()) {
<a name="line178"></a>    this.setMinWidth(x);
<a name="line179"></a>    transformed = true;
<a name="line180"></a>  }
<a name="line181"></a>
<a name="line182"></a>  var y = element.getMaxY();
<a name="line183"></a>  if (y &gt; this.getHeight()) {
<a name="line184"></a>    this.setMinHeight(y);
<a name="line185"></a>    transformed = true;
<a name="line186"></a>  }
<a name="line187"></a>
<a name="line188"></a>  return transformed;
<a name="line189"></a>};
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>/**
<a name="line193"></a> * @return {number} The width of the element&#39;s coordinate space.
<a name="line194"></a> */
<a name="line195"></a>goog.graphics.ext.Group.prototype.getCoordinateWidth = function() {
<a name="line196"></a>  return this.getWidth();
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * @return {number} The height of the element&#39;s coordinate space.
<a name="line202"></a> */
<a name="line203"></a>goog.graphics.ext.Group.prototype.getCoordinateHeight = function() {
<a name="line204"></a>  return this.getHeight();
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Remove all drawing elements from the group.
<a name="line210"></a> */
<a name="line211"></a>goog.graphics.ext.Group.prototype.clear = function() {
<a name="line212"></a>  while (this.children_.length) {
<a name="line213"></a>    this.removeChild(this.children_[0]);
<a name="line214"></a>  }
<a name="line215"></a>};
</pre>


</body>
</html>
